﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;
using System.Drawing;
using Wicresoft.UnifyShow.Portal.Extensions;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class ServiceDeskController : BaseController
    {
        // GET: /ServiceDesk/
        private IServiceDesk serviceDesk;
        private IIncident incident;
        private DateTime sTimes;
        private DateTime eTimes;
        Chart chart1 = new Chart();
        Series series1 = new Series();
        ChartArea chartArea1 = new ChartArea();
        protected override void Dispose(bool disposing)
        {
            ((IDisposable)serviceDesk).Dispose();
            ((IDisposable)incident).Dispose();
        }
        [InjectionConstructor]
        public ServiceDeskController(IServiceDesk e, IIncident iIcident)
        {
            serviceDesk = e;
            incident = iIcident;
        }
        //时间转换和为空处理
        private void timeSet(string sTime, string eTime)
        {
            sTimes = Convert.ToDateTime(sTime);
            eTimes = Convert.ToDateTime(eTime);
            if (sTime == null)
                sTimes = GlobalHelper.starTime;
            if (eTime == null)
                eTimes = GlobalHelper.endTime;
        }
        //事件来源(饼图)
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult IncidentSource(int? sid, int? bnid, string sTime, string eTime)
        {
            //时间转换和为空处理
            timeSet(sTime, eTime);
            List<IncidentAndChangeModel> lpm = serviceDesk.IncidentSourceBySite(sid, bnid, sTimes, eTimes);
            if (lpm.FirstOrDefault(p => p.ID == 2000) == null)
            {
                lpm.Add(new IncidentAndChangeModel { alls = 0, ID = 2000 });
            }
            if (lpm.FirstOrDefault(p => p.ID == 3000) == null)
            {
                lpm.Add(new IncidentAndChangeModel { alls = 0, ID = 3000 });
            }
            if (lpm.FirstOrDefault(p => p.ID == 4000) == null)
            {
                lpm.Add(new IncidentAndChangeModel { alls = 0, ID = 4000 });
            }
            if (lpm.FirstOrDefault(p => p.ID == 4201) == null)
            {
                lpm.Add(new IncidentAndChangeModel { alls = 0, ID = 4201 });
            }
            if (lpm.FirstOrDefault(p => p.ID == 6000) == null)
            {
                lpm.Add(new IncidentAndChangeModel { alls = 0, ID = 6000 });
            }
            if (lpm.FirstOrDefault(p => p.ID == 10000) == null)
            {
                lpm.Add(new IncidentAndChangeModel { alls = 0, ID = 10000 });
            }
            List<double> lyval = new List<double>();
            List<string> lxval = new List<string>();
            List<string> llegend = new List<string>();
            double sum = (double)lpm.Sum(p => p.alls);
            foreach (var ResultSort in lpm)
            {
                double percent = (double)ResultSort.alls / sum;
                lyval.Add(percent);
                string percents = Convert.ToString(((percent * 100).ToString("0.00")) + "%");
                lxval.Add(Convert.ToString(percents));
                switch (ResultSort.ID.ToString())
                {
                    case "2000": llegend.Add("电子邮件");
                        break;
                    case "3000": llegend.Add("监控系统");
                        break;
                    case "4000": llegend.Add("书面文档");
                        break;
                    case "4201": llegend.Add("自服务门户");
                        break;
                    case "6000": llegend.Add("电话");
                        break;
                    case "10000": llegend.Add("其他");
                        break;
                }
            }
            //Series
            double[] yval = lyval.ToArray();
            string[] xval = lxval.ToArray();
            string[] legend = llegend.ToArray();
            series1.ChartType = SeriesChartType.Pie;
            series1.Points.DataBindXY(xval, yval);
            for (int i = 0; i < legend.Length; i++)
            {
                series1.Points[i].LegendText = legend[i];
            }
            //分割饼图
            //series1.Points[0]["Exploded"] = "true";
            // 设置标签风格
            //series1["PieLabelStyle"] = "Outside";
            //图样式 concave/SoftEdge
            series1["PieDrawingStyle"] = "SoftEdge";
            //ChartArea
            chartArea1.AxisY.LabelStyle.Format = "0%";
            //Chart
            chart1.Width = GlobalHelper.Width;
            chart1.Height = GlobalHelper.Height;
            Legend l = new Legend();
            l.BorderColor = Color.Black;
            chart1.Legends.Add(l);
            //设置图列样式颜色
            chart1.Legends[0].Font = GlobalHelper.fontLegend;
            //Title t = new Title("事件来源");
            //t.Font = GlobalHelper.fontTitle;
            //chart1.Legends[0].FontColorCombo = Color.Red;
            //设置图颜色
            //chart1.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.SixColorSet;
            chart1.PaletteCustomColors = colorSet;
            chart1.Width = GlobalHelper.Width;
            chart1.Height = GlobalHelper.Height;
            //chart1.Titles.Add(t);
            chart1.Series.Add(series1);
            chart1.ChartAreas.Add(chartArea1);
            return new ChartResult(chart1);
        }

        //服务台事件解决率(饼图)
        [OutputCache(CacheProfile = "Report", VaryByParam = "sid;bnid;sTime;eTime")]
        public ActionResult IncidentResolvedByServiceDesk(int? sid, int? bnid, string sTime, string eTime)
        {
            //时间转换和为空处理
            timeSet(sTime, eTime);
            //事件已解决总数量
            Decimal imAll = incident.GetApproveCountIncident(sid, bnid, sTimes, eTimes);
            //服务台解决数量
            Decimal imService = serviceDesk.GetApproveCountIncidentbyResolvedGroup(sid, bnid, sTimes, eTimes);
            //获取不由服务台解决数百分比
            double AppNum = ((double)imAll - (double)imService) / (double)imAll;
            //获取服务台解决数百分比
            double ServiceDeskNum = (double)imService / (double)imAll;
            AppNum = Convert.ToDouble(AppNum);
            ServiceDeskNum = Convert.ToDouble(ServiceDeskNum);
            //Series
            series1.ChartType = SeriesChartType.Pie;

            double[] yval = { ServiceDeskNum, AppNum };
            string[] xval = { (ServiceDeskNum * 100).ToString("0.00") + "%", (AppNum * 100).ToString("0.00") + "%" };
            series1.Points.DataBindXY(xval, yval);
            series1.Points[0].LegendText = "服务台解决率";
            series1.Points[1].LegendText = "非服务台解决率";
            //图样式 concave/SoftEdge
            series1["PieDrawingStyle"] = "SoftEdge";
            //分割饼图
            //series1.Points[0]["Exploded"] = "true";
            //// 设置标签风格
            //series1["PieLabelStyle"] = "Outside";
            //ChartArea
            chartArea1.Name = "Default";
            chartArea1.AxisY.LabelStyle.Format = "0%";
            //Chart
            //添加图列
            Legend l = new Legend();
            l.BorderColor = Color.Black;
            chart1.Legends.Add(l);
            //设置图列样式颜色
            chart1.Legends[0].Font = GlobalHelper.fontLegend;
            chart1.Width = GlobalHelper.Width;
            chart1.Height = GlobalHelper.Height;
            //Title t = new Title("服务台事件解决率");
            //t.Font = GlobalHelper.fontTitle;
            //chart1.Legends[0].FontColorCombo = Color.Red;
            //设置图颜色
            //chart1.Palette = ChartColorPalette.Pastel;
            //此属性设置为None才可以自定义调色板
            chart1.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
            // 自定义调色板
            Color[] colorSet = GlobalHelper.TwoSetPei;
            chart1.PaletteCustomColors = colorSet;
            //chart1.Titles.Add(t);
            chart1.Series.Add(series1);
            chart1.ChartAreas.Add(chartArea1);
            return new ChartResult(chart1);
        }
    }
}
